package com.example.bookmall.controller;


import com.example.bookmall.entity.Result;
import com.example.bookmall.entity.usercore.LoginData;
import com.example.bookmall.entity.usercore.RegisterDTO;
import com.example.bookmall.service.Captcha.CaptchaService;
import com.example.bookmall.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    UserService userService;
    @Autowired
    CaptchaService captchaService;

    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerData) {
        return userService.register(registerData);
    }

    @PostMapping("/login")
    public Result login(@RequestBody LoginData loginData, HttpSession session, HttpServletRequest httpRequest) {

        // 先验证验证码
        if (!captchaService.validateCaptcha(httpRequest , loginData.getCaptcha())) {
            return Result.error("验证码错误或已过期");
        }

        return userService.login(loginData);
    }
} 